package main

import (
	"fmt"
	"strconv"
	"sync"
	"time"
)

/*
*
  - 加锁
    锁要支持超时
    不同任务有优先级判断，如果A任务优先级高，运行时如果B任务遇见A任务在执行，则跳过执行
*/
var lock sync.Mutex
var lastTaskType string

func main2() {
	println("程序启动")
	for i := 0; i < 5; i++ {
		go taskA(strconv.Itoa(i))
	}
	for i := 0; i < 5; i++ {
		go taskB(strconv.Itoa(i))
	}
	time.Sleep(50 * time.Second)
	println("程序结束")

}

func taskA(taskName string) {
	doTask("taskA", taskName)
}

func taskB(taskName string) {
	doTask("taskB", taskName)
}

func doTask(taskType string, taskName string) {
	lock.Lock()
	defer lock.Unlock()
	if lastTaskType == "taskA" && taskType == "taskB" {
		lastTaskType = taskType
		println(fmt.Sprintf("%s %s %s 跳过执行", time.Now().Format(time.Stamp), taskType, taskName))
		return
	}
	println(fmt.Sprintf("%s %s %s 开始执行", time.Now().Format(time.Stamp), taskType, taskName))
	time.Sleep(5 * time.Second)
	lastTaskType = taskType
	println(fmt.Sprintf("%s %s %s 结束执行", time.Now().Format(time.Stamp), taskType, taskName))
}
